﻿using SuperX.Common.Object;
using SuperX.Helper.PageBreak;
using SuperX.Helper.ServiceResponse;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperX.DeviceService.Ledger
{
    /// <summary>
    /// 台账管理
    /// </summary>
    internal class EquipmentManager
    {
        private DeviceConfig _config;
        private Dictionary<string, EquipmentLedger> equipment_dic;

        public EquipmentManager(DeviceConfig config)
        {
            _config = config;
            equipment_dic = _config.EquipmentLedgers;
        }

        /// <summary>
        /// 获取设备台账
        /// </summary>
        /// <returns></returns>
        public PageResult<EquipmentLedger> GetEquipments(EquipmentQueryInput input)
        {
            var equipments = _config.EquipmentLedgers.Values.ToList();
            equipments = !string.IsNullOrEmpty(input.SearchValue)
                    ? equipments
                    .Where(a => (!string.IsNullOrEmpty(a.name) && a.name.Contains(input.SearchValue)) || (!string.IsNullOrEmpty(a.fullName) && a.fullName.Contains(input.SearchValue)) || (!string.IsNullOrEmpty(a.type_name) && a.type_name.Contains(input.SearchValue)))
                    .ToList() : equipments;
            PageResult<EquipmentLedger> res = new PageResult<EquipmentLedger>()
            {
                PageNo = input.PageNO,
                PageSize = input.PageSize,
                TotalPage = input.TotalPage,
                TotalRows = input.TotalRows,
                Rows = equipments,
            };
            return DataPaging.SplitList(res);
        }

        /// <summary>
        /// 增加设备
        /// </summary>
        /// <param name="equipmentLedger"></param>
        /// <returns></returns>
        public ApiResponse<bool> Add(EquipmentLedger equipmentLedger)
        {
            ApiResponse<bool> res = new ApiResponse<bool>();
            if (!equipment_dic.ContainsKey(equipmentLedger.code))
            {
                equipment_dic[equipmentLedger.code] = equipmentLedger;
                res.Data = true;
                return res;
            }
            return ApiResponse<bool>.Fail("设备已存在");
        }

        /// <summary>
        /// 移除设备
        /// </summary>
        /// <param name="equipmentLedger"></param>
        /// <returns></returns>
        public ApiResponse<bool> Remove(string code)
        {
            ApiResponse<bool> res = new ApiResponse<bool>();
            if (equipment_dic.ContainsKey(code))
            {
                equipment_dic.Remove(code);
                res.Data = true;
                return res;
            }
            return ApiResponse<bool>.Fail("无法找到该设备");
        }

        /// <summary>
        /// 批量移除设备
        /// </summary>
        /// <param name="equipmentLedger"></param>
        /// <returns></returns>
        public ApiResponse<bool> BatchRemove(List<string> codes)
        {
            ApiResponse<bool> res = new ApiResponse<bool>();
            foreach (var item in codes)
            {
                if (equipment_dic.ContainsKey(item))
                {
                    equipment_dic.Remove(item);
                    res.Data = true;
                }
            }
            if (!res.Data) return ApiResponse<bool>.Fail("无可删设备！");
            return res;
        }

        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="equipmentLedger"></param>
        /// <returns></returns>
        public ApiResponse<bool> Update(string code, EquipmentLedger equipmentLedger)
        {
            ApiResponse<bool> res = new ApiResponse<bool>();
            if (equipment_dic.ContainsKey(code))
            {
                equipment_dic.Remove(code);
                equipment_dic[equipmentLedger.code] = equipmentLedger;
                res.Data = true;
                return res;
            }
            return ApiResponse<bool>.Fail("无法找到该设备");
        }

        /// <summary>
        /// 批量更新或新增
        /// </summary>
        /// <param name="equipmentLedgers"></param>
        /// <returns></returns>
        public ApiResponse<bool> BatchUpdate(List<EquipmentLedger> equipmentLedgers)
        {
            ApiResponse<bool> res = new ApiResponse<bool>();
            if (!equipmentLedgers.Any())
            {
                return ApiResponse<bool>.Fail("无更新数据！");
            }
            foreach (var item in equipmentLedgers)
            {
                equipment_dic[item.code] = item;
            }
            res.Data = true;
            return res;
        }

        /// <summary>
        /// 清空台账集合
        /// </summary>
        /// <returns></returns>
        public ApiResponse<bool> Clear()
        {
            ApiResponse<bool> res = new ApiResponse<bool>();
            if (equipment_dic != null)
            {
                equipment_dic.Clear();
                res.Data = true;
                return res;
            }
            return ApiResponse<bool>.Fail("清空失败！");
        }
    }
}